package controller;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;

import com.fasterxml.jackson.databind.util.JSONPObject;

import vo.Item;
import vo.User;

@Controller
//@SessionAttributes(names={"user"})
public class JohnController {
	
	public JohnController() {
		
		System.out.println("john contructor.....");
	}
	@RequestMapping("/login123")
	@ResponseBody
	public User login123(String uname){
		System.out.println(uname);
		User u = new User();
		u.setId(123);u.setUname(uname);
		return u;
	}
	

	@RequestMapping(value={"/ll"})
	@ResponseBody
	public JSONPObject login(String uname,String callback){
		System.out.println(callback);
		User u = new User();
		u.setId(100);u.setUname("john");
		JSONPObject o=new JSONPObject(callback, u);
		return  o;
	}
	@RequestMapping(value={"user/logAfter"})
	public String loginAfter(String uname){
		System.out.println("logAfter...");
		return  "index";
	}
	
	@RequestMapping(value={"/regist1"},method={RequestMethod.POST}
	,consumes="application/json",produces="application/json")
	@ResponseBody
	public User regist(@RequestBody User u){
		System.out.println(u.getId()+"....///.....");
		return u;
	}
	@RequestMapping("/test1/user/{uid}/book/{bid:[a-d]+}")
	public String test1(@PathVariable("uid") int id,@PathVariable String bid){
		System.out.println(id+"....."+bid);
		return "index";
	}
	@RequestMapping("/test2")
	public String test2( Map<String, String> map){
		map.put("ids", "120");
		System.out.println(map.get("uname1"));
		return "index1";
	}
//	@ModelAttribute(name="user")
//	public User addAccount(int id){
//		System.out.println("add account...."+id);
//		return new User();
//	}
	@RequestMapping("/test3")
	public String test3( User user,BindingResult rs){
		System.out.println(user.getId()+"........."+user.getBirth());
		Object o=rs.getFieldError("birth").getRejectedValue();
		Object o1=rs.getFieldError("birth").getDefaultMessage();
		
		System.out.println(o+"....."+o1);
		if(rs.hasErrors()){
			return "index2";
		}
		return "index1";
	}
	@RequestMapping("/upload")
	public String upload(User u) throws Exception{
		System.out.println(u.getUname());
		System.out.println(u.getImg().getOriginalFilename()+"...."+u.getImg().getBytes().length);
		return "johnYu";
	}
	
//	@ModelAttribute("s")
//	public String addAccount(){
//		System.out.println("add account.....");
//		return "account";
//	}
	
}
